package com.itheima.controller.backend.security.admin;

import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.Result;
import com.itheima.common.pojo.Order;
import com.itheima.common.pojo.OrderSetting;
import com.itheima.common.pojo.dto.OrderSettingPageQueryDTO;
import com.itheima.service.OrderService;
import com.itheima.service.OrderSettingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@RestController
@RequestMapping("/ordersetting")
@Slf4j
public class OrderSettingController {

    @Autowired
    private OrderSettingService orderSettingService;
    @Autowired
    private OrderService orderService;

    /**
     * 根据月份查询预约设置数据
     * @param month
     * @return
     */
    @GetMapping("/getOrderSettingByMonth")
    public Result getOrderSettingByMonth(@RequestParam String month) {
        try {
            List<Map<String, Object>> orderSettingByMonth = orderSettingService.getOrderSettingByMonth(month);
            return new Result(true, MessageConstant.QUERY_ORDER_SUCCESS, orderSettingByMonth);
        } catch (Exception e) {
            log.error(MessageConstant.QUERY_ORDER_FAIL, e);
            return new Result(false, MessageConstant.QUERY_ORDER_FAIL);
        }
    }


    /**
     * 根据日期修改可预约人数
     * @param orderSetting
     */
    @PostMapping("/editNumberByOrderDate")
    public Result editNumberByDate(@RequestBody OrderSetting orderSetting) {
        try {
            // 确保orderSetting对象的完整性,使用Objects.isNull()来检查null值
            if (Objects.isNull(orderSetting.getReservations())) {
                orderSetting.setReservations(0);
            }
            orderService.updateNumberByDate(orderSetting);
            return new Result(true, MessageConstant.ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            log.error("修改预约设置失败", e);
            throw new RuntimeException(MessageConstant.ORDERSETTING_FAIL);
        }
    }


    /**
     * 分页查询预约列表
     * @param orderSettingDTO
     * @return
     */
    @PostMapping("/findPage")
    public PageResult findPage(@RequestBody OrderSettingPageQueryDTO orderSettingDTO){
        log.info("分页查询预约列表:{}",orderSettingDTO);
        PageResult pageResult = orderSettingService.pageQuery(orderSettingDTO);
        return pageResult;
    }

    /**
     * 导出预约设置数据
     * @param response
     */
    @GetMapping("/download")
    public void download(HttpServletResponse response){
        orderSettingService.dowload(response);
    }

    /**
     * 导入预约设置数据
     * @param file
     * @return
     */
    @PostMapping("/upload")
    public Result importExcel(@RequestParam("excelFile") MultipartFile file){
        try {
            orderSettingService.importExcel(file);
            return new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            log.error(MessageConstant.IMPORT_ORDERSETTING_FAIL, e);
            return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }
    }

}
